package org.example.system.common.utils;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.Serializable;

/**
 * 响应操作工具类
 * @author joey
 */
@Data
@ApiModel(value="响应对象")
public class ResponseUtil<T> implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(ResponseUtil.class);

    private static final long serialVersionUID = 1L;
    public static final String UTF_8 = "UTF-8";
    public static final String APPLICATION_JSON_CHARSET_UTF_8 = "application/json; charset=utf-8";
    public static int SUCCESS_20000=20000;
    public static final String SUCCESS_STR="请求成功";
    public static int ERROR_50000=50000;
    public static final String ERROR_STR="请求失败";
    public static int ERROR_50001=50001;
    public static final String ERROR_STR1="请求服务失败";
    public static final int ERROR_40301=40301;
    public static final String ERROR_AUTH1="token不存在";
    public static final int ERROR_40302=40302;
    public static final String ERROR_AUTH2="token已过期";
    public static final int ERROR_40303=40303;
    public static final String ERROR_AUTH3="无权进行该操作";
    public static final int ERROR_40304=40304;
    public static final String ERROR_AUTH4="用户名或密码不能为空";
    public static final int ERROR_40305=40305;
    public static final String ERROR_AUTH5="用户名或密码错误";

    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "提示内容")
    private String msg;
    @ApiModelProperty(value = "响应数据")
    private T data;


    public static ResponseUtil success(Object obj){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(true);
        dto.setMsg(SUCCESS_STR);
        dto.setCode(SUCCESS_20000);
        dto.setData(obj);
        return dto;
    }
    public static ResponseUtil success(){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(true);
        dto.setMsg(SUCCESS_STR);
        dto.setCode(SUCCESS_20000);
        return dto;
    }
    public static ResponseUtil error(String msg){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(msg);
        dto.setCode(ERROR_50000);
        return dto;
    }
    public static ResponseUtil error(){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(ERROR_STR);
        dto.setCode(ERROR_50000);
        return dto;
    }
    public static ResponseUtil feignFail(){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(ERROR_STR1);
        dto.setCode(ERROR_50001);
        return dto;
    }
    public static ResponseUtil feignFail(Object obj){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(ERROR_STR1);
        dto.setCode(ERROR_50001);
        dto.setData(obj);
        return dto;
    }
    /**
     * 用户名或密码错误
     * @return
     */
    public static ResponseUtil noAuthority5(){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(ERROR_AUTH5);
        dto.setCode(ERROR_40305);
        return dto;
    }

    /**
     * 用户名或密码为空
     * @return
     */
    public static ResponseUtil noAuthority4(){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(ERROR_AUTH4);
        dto.setCode(ERROR_40304);
        return dto;
    }

    /**
     * 没有权限
     * @return
     */
    public static ResponseUtil noAuthority3(){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(ERROR_AUTH3);
        dto.setCode(ERROR_40303);
        return dto;
    }

    /**
     * token已过期
     * @return
     */
    public static ResponseUtil noAuthority2(){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(ERROR_AUTH2);
        dto.setCode(ERROR_40302);
        return dto;
    }

    /**
     * token不存在
     * @return
     */
    public static ResponseUtil noAuthority1(){
        ResponseUtil dto = new ResponseUtil();
        dto.setSuccess(false);
        dto.setMsg(ERROR_AUTH1);
        dto.setCode(ERROR_40301);
        return dto;
    }

    public static void handle(HttpServletResponse response, Object dto){
        response.setCharacterEncoding(UTF_8);
        response.setContentType(APPLICATION_JSON_CHARSET_UTF_8);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(JSONUtil.toJsonStr(dto));
        }catch (Exception e){
            logger.error("响应返回出错",e);
        }finally {
            if(out!=null){
                out.close();
            }
        }
    }
    public static void handle(Object dto){
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setCharacterEncoding(UTF_8);
        response.setContentType(APPLICATION_JSON_CHARSET_UTF_8);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(JSONUtil.toJsonStr(dto));
        }catch (Exception e){
            logger.error("响应返回出错",e);
        }finally {
            if(out!=null){
                out.close();
            }
        }
    }



}
